function arraySum(arr) {
  return arr.reduce((total, val) => (total += val));
}

const arr = [1, 2, 3, 4, 5, 6];
const res = arraySum(arr);
// console.log(res)

function arraySum2(arr) {
  //   return arr.flat(Infinity).reduce((total, val) => (total += val));
  arr
    .toString()
    .split(",")
    .reduce((total, val) => (total += Number(val)), 0);
  return arr;
}

const arr2 = [1, 2, 3, [[4, 5], 6], 7, 8, 9];
const res2 = arraySum2(arr2);
// console.log(res2);

function arraySum3(arr) {
  if (arr.length === 1)
    return arr[0] instanceof Array ? arraySum3(arr[0]) : arr[0];
  return arr[0] instanceof Array
    ? arraySum3(arr[0]) + arraySum3(arr.slice(1))
    : arr[0] + arraySum3(arr.slice(1));
}

const res3 = arraySum3(arr2);
console.log(res3);
